var app = getApp()
var reasons
var ordertype
var orderSource

Page({
  data: {},
  onLoad: function (options) {
    if (options.orderSource) {
      orderSource = options.orderSource
    }
    if (options.order && options.type) {
      ordertype = options.type
      var status
      if (options.status) {
        status = options.status
      }
      else {
        status = ''
      }
      var order = JSON.parse(decodeURIComponent(options.order))
      var dispdate = order.orderformOrderBillDOs[0].orderformOrderDO.DISPDATE
      var disptime = order.orderformOrderBillDOs[0].orderformOrderDO.DISPTIME
      order.orderformBatchDO.DISCOUNT = parseFloat(order.orderformBatchDO.DISCOUNT)
      order.orderformBatchDO.PROMOTIONINFO = JSON.parse(order.orderformBatchDO.PROMOTIONINFO)
      dispdate = dispdate.replace('00:00:00', disptime)
      this.setData({
        order: order,
        status: status,
        dispdate: dispdate,
        target: order.orderformOrderBillDOs[0].orderformOrderDO.STATUS
      })
    }
  },
  cancelOrder: function () {
    if (ordertype === 'bread') {
      this.cancelOrder2()
    }
    else {
      this.cancelOrder1()
    }
  },
  cancelOrder1: function () {
    wx.showModal({
      title: '',
      content: '蛋糕订单请联系客服取消。客服电话：400-992-6632',
      confirmText: '我知道了',
      confirmColor: '#a08257',
      showCancel: false
    })
  },
  cancelOrder2: function () {
    var that = this
    wx.showModal({
      title: '',
      content: '是否取消订单？',
      confirmText: '是',
      confirmColor: '#a08257',
      cancelText: '否',
      success: function (res) {
        if (res.confirm) {
          that.didCancelAction1()
        }
      }
    })
  },
  didCancelAction1: function () {
    var url = '/yuanmai_we/orderform/order/cancel/reason'
    app.get(url).then(res => {
      reasons = res
      var reasons_ch = []
      for (var i = 0; i < reasons.length; i++) {
        reasons_ch.push(reasons[i].REASON)
      }
      var that = this
      wx.showActionSheet({
        itemList: reasons_ch,
        success: function (res) {
          that.didCancelAction2(res.tapIndex)
        }
      })
    })
  },
  didCancelAction2: function (index) {
    var url = '/yuanmai_we/orderform/orderForm/cancel/'
    var orderno = this.data.order.orderformOrderBillDOs[0].id
    url = url + orderno
    var params = {
      REASON: reasons[index].REASON,
      __zaofans: true
    }
    app.request(url, params).then(res => {
      if (res.success === true) {
        wx.navigateBack()
      }
    })
  },
  getEvaluate: function () {
    wx.navigateTo({
      url: '../evaluate/evaluate?orderId=' + this.data.order.orderformOrderBillDOs[0].id + '&orderSource=' + orderSource
    })
  }
})
